package com.dxtan90.util;


import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;
import java.util.Properties;

/**
 * @author shijunhao
 * @since 2024/4/29 17:19
 */
public class PropertiesUtil {

    protected static final Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);

    private static final Properties properties;

    // 静态初始化参数
    static {
        properties = new Properties();
        InputStream inputStream = null;
        try {
            // 获取配置文件路径
            String path = Objects.requireNonNull(PropertiesUtil.class.getClassLoader().getResource("config.properties")).getPath();
            // 创建输入流
            inputStream = new FileInputStream(path);
            // 读取配置文件
            properties.load(inputStream);
        } catch (IOException e) {
            logger.error("读取配置文件失败", e);
        } finally {
            IOUtils.closeQuietly(inputStream);
        }
    }

    // 根据key获取value的方法
    public static String getProperty(String key) {
        return properties.getProperty(key);
    }
}

